package webapi

import (
	"MqttCloudProtocolServer/publiclib/dbcontrol"
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/wonderivan/logger"
)

//UserManagerService 网关服务器注册API类。注册控制台
type UserManagerService struct {
	base ServiceBase
}

//Regist 注册
func (_this *UserManagerService) Regist(mux *http.ServeMux) {
	_this.base.Regist(mux)
	_this.base.path += "ums"
	_this.base.HandleRegist("addUser", _this.addUser)
	_this.base.HandleRegist("getUsers", _this.getUsers)
	_this.base.HandleRegist("deleteUser", _this.deleteUser)
	_this.base.HandleRegist("saveUser", _this.saveUser)
}

//addUser 注册
func (_this *UserManagerService) addUser(w http.ResponseWriter, r *http.Request) {
	vars := r.URL.Query()
	if len(vars) > 0 {
		user := GetParam(vars, "user")
		password := GetParam(vars, "password")
		control := dbcontrol.CreateUserconfControl()
		if control == nil {
			logger.Alert("无法获取数据库控制器")
			fmt.Fprintln(w, "Failed")
			return
		}
		if err := control.FindByName(user); err != nil {
			fmt.Fprintln(w, "Failed")
			return
		} else if control.Data != nil {
			fmt.Fprintln(w, "用户已存在。请换一个用户名")
			return
		}
		control.CreateNew()
		control.Data.Name = user
		control.Data.Password = password
		control.Data.Level = 1
		if err := control.Save(); err != nil {
			fmt.Fprintln(w, "Failed")
			return
		}
		fmt.Fprintln(w, "Success")
		return
	}
	fmt.Fprintln(w, "Failed")
	//return
}

//getUsers 注册
func (_this *UserManagerService) getUsers(w http.ResponseWriter, r *http.Request) {
	control := dbcontrol.CreateUserconfControl()
	if control == nil {
		logger.Alert("无法获取数据库控制器")
		fmt.Fprintln(w, "Failed")
		return
	}
	users, err := control.GetAll()
	if err != nil {
		logger.Alert(err)
		fmt.Fprintln(w, "Failed")
		return
	}
	bytes, err2 := json.Marshal(users)
	if err2 != nil {
		logger.Alert(err2)
		fmt.Fprintln(w, "Failed")
		return
	}
	fmt.Fprintln(w, string(bytes[:]))
	//return
}

//deleteUser 删除用户
func (_this *UserManagerService) deleteUser(w http.ResponseWriter, r *http.Request) {
	vars := r.URL.Query()
	if len(vars) > 0 {
		user := GetParam(vars, "user")
		control := dbcontrol.CreateUserconfControl()
		if control == nil {
			logger.Alert("无法获取数据库控制器")
			fmt.Fprintln(w, "Failed")
			return
		}
		if err := control.FindByName(user); err != nil || control.Data == nil {
			logger.Alert(err)
			fmt.Fprintln(w, "Failed")
			return
		}
		if err := control.Delete(); err != nil {
			logger.Alert(err)
			fmt.Fprintln(w, "Failed")
			return
		}
		fmt.Fprintln(w, "Success")
		return
	}
	fmt.Fprintln(w, "Failed")
	//return
}

//saveUser 保存修改的用户信息
func (_this *UserManagerService) saveUser(w http.ResponseWriter, r *http.Request) {
	vars := r.URL.Query()
	if len(vars) > 0 {
		user := GetParam(vars, "user")
		password := GetParam(vars, "password")
		control := dbcontrol.CreateUserconfControl()
		if control == nil {
			logger.Alert("无法获取数据库控制器")
			fmt.Fprintln(w, "Failed")
			return
		}
		if err := control.FindByName(user); err != nil || control.Data == nil {
			logger.Alert(err)
			fmt.Fprintln(w, "Failed")
			return
		}
		control.Data.Password = password
		control.Data.Level = 1
		if err := control.Save(); err != nil {
			logger.Alert(err)
			fmt.Fprintln(w, "Failed")
			return
		}
		fmt.Fprintln(w, "Success")
		return
	}
	fmt.Fprintln(w, "Failed")
	//return
}
